﻿using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Text;
using System.Xml.Linq;

namespace AspDotNet.Core6.WebApi.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    [Produces("application/json")]
    public class HttpVerbController : ControllerBase
    {
        [HttpGet]
        public IActionResult GetByFromQuery(
            [FromHeader] string appkey, [FromHeader] string appsecret,
            [FromQuery] string name, [FromQuery] string age)
        {
            Dictionary<string, string> headers = new Dictionary<string, string>();
            foreach (var item in Request.Headers)
            {
                headers.Add(item.Key, item.Value);
            }
            return Ok(new
            {
                request = new
                {
                    headers,
                    query = new
                    {
                        name = Request.Query["name"],
                        age = Request.Query["age"]
                    },
                    queryString = new
                    {
                        name = Request.QueryString.Value,
                    }
                },
                attribute = new
                {
                    name,
                    age,
                    appkey,
                    appsecret
                }
            });
        }

        [HttpPost]
        public async Task<IActionResult> PostByFormQuery(
            [FromHeader] string appkey, [FromHeader] string appsecret,
            [FromQuery] People people)
        {
            Dictionary<string, string> headers = new Dictionary<string, string>();
            foreach (var item in Request.Headers)
            {
                headers.Add(item.Key, item.Value);
            }

            string body = string.Empty;

            using (var reader = new StreamReader(Request.Body))
            {
                body = await reader.ReadToEndAsync();
            }

            return Ok(new
            {
                request = new
                {
                    headers,
                    query = new
                    {
                        name = Request.Query["name"],
                        age = Request.Query["age"],
                        json = Request.Form[""].ToString()
                    },
                    queryString = new
                    {
                        name = Request.QueryString.Value,
                    }
                },
                body,
                attribute = new
                {
                    people.name,
                    people.age,
                    appkey,
                    appsecret
                }
            });
        }

        [HttpPut]
        [Consumes("multipart/form-data", "application/x-www-form-urlencoded")]
        public async Task<IActionResult> PutByFormQuery([FromQuery] string id)
        {
            Dictionary<string, string> headers = new Dictionary<string, string>();
            foreach (var item in Request.Headers)
            {
                headers.Add(item.Key, item.Value);
            }
            string body = string.Empty;

            using (var reader = new StreamReader(Request.Body))
            {
                body = await reader.ReadToEndAsync();
            }
            return Ok(new
            {
                request = new
                {
                    headers,
                    query = new
                    {
                        id = Request.Query["id"],
                    },
                    queryString = new
                    {
                        name = Request.QueryString.Value,
                    }
                },
                form = new
                {
                    name= Request.Form["name"].ToString(),
                    age = Request.Form["age"].ToString(),
                    json = Request.Form[""].ToString()
                },
                body,
                attribute = new
                {
                    id
                }
            });
        }


        [HttpDelete]
        [Consumes("multipart/form-data", "application/x-www-form-urlencoded")]
        public async Task<IActionResult> DeleteByFormQuery([FromQuery] string id)
        {
            Dictionary<string, string> headers = new Dictionary<string, string>();
            foreach (var item in Request.Headers)
            {
                headers.Add(item.Key, item.Value);
            }
            string body = string.Empty;

            using (var reader = new StreamReader(Request.Body))
            {
                body = await reader.ReadToEndAsync();
            }
            return Ok(new
            {
                request = new
                {
                    headers,
                    query = new
                    {
                        id = Request.Query["id"],
                    },
                    queryString = new
                    {
                        name = Request.QueryString.Value,
                    }
                },
                form = new
                {
                    name = Request.Form["name"].ToString(),
                    age = Request.Form["age"].ToString(),
                    json = Request.Form[""].ToString()
                },
                body,
                attribute = new
                {
                    id
                }
            });
        }

        [HttpPatch]
        [Consumes("multipart/form-data", "application/x-www-form-urlencoded")]
        public async Task<IActionResult> PatchByFromForm([FromQuery] string id, [FromForm] People people)
        {
            Dictionary<string, string> headers = new Dictionary<string, string>();
            foreach (var item in Request.Headers)
            {
                headers.Add(item.Key, item.Value);
            }
            string body = string.Empty;

            using (var reader = new StreamReader(Request.Body))
            {
                body = await reader.ReadToEndAsync();
            }
            return Ok(new
            {
                request = new
                {
                    headers,
                    query = new
                    {
                        id = Request.Query["id"],
                    },
                    queryString = new
                    {
                        name = Request.QueryString.Value,
                    }
                },
                form = new
                {
                    name = Request.Form["name"].ToString(),
                    age = Request.Form["age"].ToString(),
                    json = Request.Form[""].ToString()
                },
                body,
                attribute = new
                {
                    id
                }
            });
        }
    }
}
